package org.anupta.game.entity.entities;

import java.util.Map;
import java.util.TreeMap;

import javax.swing.Timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.anupta.game.entity.dao.DAO;
import org.anupta.game.entity.entities.Item.Position;

public class Player extends Entity {
	private int guid = 0;
	private String name = null;
	private int gameId = 0;
	private Sexe sexe = null;
	private Classe classe = null;
	private int level = 0;
	private int gfx = 0;
	private int[] colors = {0,0,0};
	private boolean isNew = false;
	private Map<Byte,Item> stuff = new TreeMap<Byte,Item>();
	private int life = 0;
	private String channels = null;
	private Timer sitTimer;
	private boolean hasTalk = false;

	public Player(int guid,String name,int gameId,int sexe,int classe,int level,int gfx,int color1,int color2, int color3,boolean isNew,String stuff,int life,String channels) {
		setGuid(guid);
		setName(name);
		setGameId(gameId);
		setSexe(sexe);
		setClasse(classe);
		setLevel(level);
		setGfx(gfx);
		setColors(new int[]{color1,color2,color3});
		setNew(isNew);
		setStuff(stuff);
		setLife(life);
		setChannels(channels);
		sitTimer = new Timer(2000,new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				regenLife();
			}
		});
	}

	public String toALK()
	{
		StringBuilder perso = new StringBuilder();
		perso.append("|");
		perso.append(guid).append(";");
		perso.append(name).append(";");
		perso.append(level).append(";");
		perso.append(gfx).append(";");
		perso.append((colors[0]!= -1?Integer.toHexString(colors[0]):"-1")).append(";");
		perso.append((colors[1]!= -1?Integer.toHexString(colors[1]):"-1")).append(";");
		perso.append((colors[2]!= -1?Integer.toHexString(colors[2]):"-1")).append(";");
		perso.append(getStuffToALK()).append(";");
		//perso.append((this.is_showSeller()?1:0)).append(";");
		perso.append("0;");
		perso.append("1;");//TODO: What is that?
		perso.append(";");//DeathCount TODO: For heroic mode
		perso.append(";");//LevelMax TODO: Get level max from DB
		return perso.toString();
	}
	
	public String getStuffToALK(){
		StringBuilder alk = new StringBuilder();
		if(getItemByPos(Position.ARME) != null){
			alk.append(Integer.toHexString(getItemByPos(Position.ARME).getTemp().getGuid()));
		}
		alk.append(",");
		if(getItemByPos(Position.COIFFE) != null){
			alk.append(Integer.toHexString(getItemByPos(Position.COIFFE).getTemp().getGuid()));
		}
		alk.append(",");
		if(getItemByPos(Position.CAPE) != null){
			alk.append(Integer.toHexString(getItemByPos(Position.CAPE).getTemp().getGuid()));
		}
		alk.append(",");
		if(getItemByPos(Position.FAMILIER) != null){
			alk.append(Integer.toHexString(getItemByPos(Position.FAMILIER).getTemp().getGuid()));
		}
		alk.append(",");
		if(getItemByPos(Position.BOUCLIER) != null){
			alk.append(Integer.toHexString(getItemByPos(Position.BOUCLIER).getTemp().getGuid()));
		}
		return alk.toString();
	}
	
	public String getStuffToASK(){
		StringBuilder ask = new StringBuilder();
		if(stuff.isEmpty()){
			return "";
		}else{
			for(Item item : stuff.values()){
				ask.append(item.getItemString());
			}
		}
		return ask.toString();
	}
	
	public Item getItemByPos(Position pos){
		for(Item item : stuff.values()){
			if(item.getPos() == pos){
				return item;
			}
		}
		return null;
	}
	
	@Override
	public int getGuid() {
		return guid;
	}

	@Override
	public void setGuid(int guid) {
		this.guid = guid;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public int getGameId() {
		return gameId;
	}

	public void setGameId(int gameId) {
		this.gameId = gameId;
	}

	public Sexe getSexe() {
		return sexe;
	}

	public void setSexe(Sexe sexe) {
		this.sexe = sexe;
	}
	
	public void setSexe(int sexe){
		this.sexe = Sexe.get((byte) sexe);
	}

	public Classe getClasse() {
		return classe;
	}

	public void setClasse(Classe classe) {
		this.classe = classe;
	}
	
	public void setClasse(int id){
		classe = Classe.get((byte) id);
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getGfx() {
		return gfx;
	}
	
	public static int getDefautGfx(byte classe,byte sexe){
		return Integer.parseInt(Byte.toString(classe) + Byte.toString(sexe));
	}

	public void setGfx(int gfx) {
		this.gfx = gfx;
	}

	public int[] getColors() {
		return colors;
	}

	public void setColors(int[] colors) {
		this.colors = colors;
	}

	public boolean isNew() {
		return isNew;
	}

	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	public Map<Byte,Item> getStuff() {
		return stuff;
	}

	public void setStuff(Map<Byte,Item> stuff) {
		this.stuff = stuff;
	}
	
	public Timer getSitTimer() {
		return sitTimer;
	}

	public void setSitTimer(Timer sitTimer) {
		this.sitTimer = sitTimer;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public String getChannels() {
		return channels;
	}

	public void setChannels(String channels) {
		this.channels = channels;
	}

	public void setStuff(String stuff){
		if(!stuff.isEmpty()){
			for(String item : stuff.split(";")){
				Item obj = DAO.getDao(Item.class).find(Integer.parseInt(item));
				if(obj != null){
					this.stuff.put(obj.getPos().getPos(), obj);
				}
			}
		}
	}
	
	public String getStuffString(){
		StringBuilder items = new StringBuilder();
		for(Item item : stuff.values()){
			if(items.length() > 0){
				items.append(";");
			}
			items.append(item.getGuid());
		}
		return items.toString();
	}
	
	public void regenLife(){
		life++;
		DAO.getDao(Player.class).update(this);
	}
	
	public boolean isHasTalk() {
		return hasTalk;
	}

	public void setHasTalk(boolean hasTalk) {
		this.hasTalk = hasTalk;
	}

	public static enum Classe {
		FECA((byte)1),
		OSAMODAS((byte)2),
		ENUTROF((byte)3),
		SRAM((byte)4),
		XELOR((byte)5),
		ECAFLIP((byte)6),
		ENIRIPSA((byte)7),
		IOP((byte)8),
		CRA((byte)9),
		SADIDA((byte)10),
		SACRIEUR((byte)11),
		PANDAWA((byte)12);
		
		private byte id = 0;
		
		private Classe(byte id){
			setId(id);
		}
		
		public static Classe get(byte id){
			switch(id){
			case 1:
				return FECA;
			case 2:
				return OSAMODAS;
			case 3:
				return ENUTROF;
			case 4:
				return SRAM;
			case 5:
				return XELOR;
			case 6:
				return ECAFLIP;
			case 7:
				return ENIRIPSA;
			case 8:
				return IOP;
			case 9:
				return CRA;
			case 10:
				return SADIDA;
			case 11:
				return SACRIEUR;
			case 12:
				return PANDAWA;
			}
			return null;
		}

		public byte getId() {
			return id;
		}

		public void setId(byte id) {
			this.id = id;
		}
	}
	public static enum Sexe{
		MASCULIN((byte)0),
		FEMININ((byte)1);
		
		private byte sexe = 0;
		
		private Sexe(byte sexe){
			setSexe(sexe);
		}
		
		public static Sexe get(byte sexe){
			switch(sexe){
			case 0:
				return MASCULIN;
			case 1:
				return FEMININ;
			}
			return null;
		}
		
		public byte getSexe(){
			return sexe;
		}
		
		public void setSexe(byte sexe){
			this.sexe = sexe;
		}
	}
}
